package view.controller;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import common.Message;
import common.NowUser;
import common.User;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import server.ClientConnServer;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class ApplyController implements Initializable {
    public Label applyname;
    public Label applyid;
    public ImageView applyimage;
    public Button applytrue;
    public Button applyfalse;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        applyname.setText(NowUser.getFriend().getName());
        applyid.setText(NowUser.getFriend().getId());
        if(NowUser.getFriend().getHeadimage()!=null && !NowUser.getFriend().getHeadimage().equals("") && !NowUser.getFriend().getHeadimage().equals("null")){
            javafx.scene.image.Image image=new javafx.scene.image.Image("file:///" + NowUser.getFriend().getHeadimage());
            applyimage.setImage(image);
        }else{
            javafx.scene.image.Image image=new Image("view/photo/默认头像.jpg");
            applyimage.setImage(image);
        }
    }

    public void onapplytrue(ActionEvent actionEvent) throws IOException , BrokenBarrierException,InterruptedException {
        User user = new User();
        Message msg = new Message();
        user.setId(NowUser.getUser().getId());
        user.setFriendid(applyid.getText());
        msg.setUser(user);
        msg.setType("acceptfriend");
        NowUser.acfriendinfo=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(NowUser.acfriendinfo){
            JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.PLAIN_MESSAGE);
            Stage stage = (Stage) applytrue.getScene().getWindow();
            stage.close();
        }else{
            System.out.println(msg.getType());
            JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void onapplyfalse(ActionEvent actionEvent) throws IOException ,BrokenBarrierException,InterruptedException{
        User user = new User();
        Message msg = new Message();
        user.setId(NowUser.getUser().getId());
        user.setFriendid(applyid.getText());
        msg.setUser(user);
        msg.setType("acceptfalsefriend");
        NowUser.rjfriendinfo=false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        if(NowUser.rjfriendinfo) {
            JOptionPane.showMessageDialog(null, "拒绝好友申请成功", "提示", JOptionPane.PLAIN_MESSAGE);
            Stage stage = (Stage) applyfalse.getScene().getWindow();
            stage.close();
        } else{
            JOptionPane.showMessageDialog(null, "拒绝好友申请失败", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }
}
